package com.baoku.wap.dao;

import java.sql.*;
import java.util.*;

import com.baoku.wap.entity.Title;
import com.baoku.wap.util.DB;

/**
 * 在cach中读取的数据
 * 
 * @author Administrator
 *
 */
public class CachFactory {
	
	private int pageCount;
	
	private int pageNo;
	
	private int lastPage;
	
	private int nextPage;
	
	public int getPageCount() {
		return pageCount;
	}
	
	public int getPageNo() {
		return pageNo;
	}

	public int getLastPage() {
		return lastPage;
	}

	public int getNextPage() {
		return nextPage;
	}
	
	private void setPage(int pageNo,int pageSize,int count){
		//计算出总页数
		this.pageCount = (int)Math.ceil((double)count/pageSize); 		
		
		//设置正确的页码
		if (pageNo > this.pageCount) {
			this.pageNo = this.pageCount;
		}else if (pageNo < 1) {
			this.pageNo = 1;
		}else {
			this.pageNo = pageNo;
		}
		
		//设置前后页,前后页超出范围也无所谓,有值钱设置的语句控制.
		this.nextPage = this.pageNo + 1;
		this.lastPage = this.pageNo - 1;	
	}
	
	//得到首页推荐问题的题目和id , 只有3个.
	public List getindexPrime (){
		List list = new ArrayList();
		
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			rs = st.executeQuery("select * from knowledge where isprime=1 limit 0,3");
			while(rs.next()){
				Title title = new Title();
				title.setId(rs.getInt("id"));
				title.setTitle(rs.getString("question"));
				list.add(title);

			}
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return list;
	}
	
	//得到首页热点问题的题目和id,只有3个.
	public List getindexPop (){
		List list = new ArrayList();
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			rs = st.executeQuery("select knowledge.id,knowledge.question from knowledge , popknowledge where knowledge.id = popknowledge.id limit 0,3");
			while(rs.next()){
				Title title = new Title();
				title.setId(rs.getInt("id"));
				title.setTitle(rs.getString("question"));
				list.add(title);

			}
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return list;
	}
	
	//在cachlist.jsp页面中得到pop问题列表,每页4个问题,需要传入pageNo,同时会设置CachFactory的各个参数.
	public List getPop (int pageNo){
		int pageSize = 4;
		
		List list = new ArrayList();
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			rs = st.executeQuery("select count(*) from popknowledge");
			int count=0;
			if(rs.next()) {count=rs.getInt(1);}
			this.setPage(pageNo,pageSize,count);
			int page = (this.pageNo-1)*pageSize; 
			
			rs = st.executeQuery("select knowledge.id,knowledge.question from knowledge , popknowledge where knowledge.id = popknowledge.id limit "+page+","+pageSize);
			while(rs.next()){
				Title title = new Title();
				title.setId(rs.getInt("id"));
				title.setTitle(rs.getString("question"));
				list.add(title);

			}
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return list;
	}
	
	//在cachlist.jsp页面中得到new问题列表,每页4个问题,需要传入pageNo,同时会设置CachFactory的各个参数.
	public List getNew (int pageNo){
		int pageSize = 4;
		
		List list = new ArrayList();
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			rs = st.executeQuery("select count(*) from knowledge where isnew =1 ");
			int count=0;
			if(rs.next()) {count=rs.getInt(1);}
			this.setPage(pageNo,pageSize,count);
			int page = (this.pageNo-1)*pageSize; 
			rs = st.executeQuery("select * from knowledge where isnew=1 limit "+page+","+pageSize);
			while(rs.next()){
				Title title = new Title();
				title.setId(rs.getInt("id"));
				title.setTitle(rs.getString("question"));
				list.add(title);

			}
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return list;
	}
}
